package wang.snalc.htmltopic;

import gui.ava.html.image.generator.HtmlImageGenerator;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

@RestController
public class TestController {


    @GetMapping("test")
    public void download( HttpServletResponse response) throws Exception {
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");
        imageGenerator.saveAsImage("d:/hello-world.png");

        InputStream inputStream = Files.newInputStream(Paths.get("d:/hello-world.png"));
        OutputStream outputStream = response.getOutputStream();
        response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=test.txt");
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();

    }
}
